package com.xnyzc.lhy.mis.controller.cmsapi.activity;


import com.xnyzc.lhy.common.entity.ActivityRv;
import com.xnyzc.lhy.common.exception.ActivityException;
import com.xnyzc.lhy.common.penum.activity.ActivityErrorCode;
import com.xnyzc.lhy.mis.entity.param.activity.ActivityCreateParam;
import com.xnyzc.lhy.mis.entity.param.activity.ActivityDefSearchParam;
import com.xnyzc.lhy.mis.entity.result.activity.ActivityDefSearchResult;
import com.xnyzc.lhy.mis.entity.result.activity.ActivityInfoResult;
import com.xnyzc.lhy.mis.service.activity.IOaSysActivityDefService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 活动定义 前端控制器
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/activity/activityDef")
@Slf4j
public class OaSysActivityDefController {

    @Autowired
    private IOaSysActivityDefService iOaSysActivityDefService;

    /**
     * 获取活动定义（含查询条件）
     *
     * @return
     */
    @ApiOperation(value = "获取活动定义（含查询条件）", notes = "获取活动定义（含查询条件）")
    @PostMapping(value = "/queryActivityDefList")
    public ActivityRv queryActivityDefList(@RequestBody ActivityDefSearchParam activityDefSearchParam) {
        try {
            List<ActivityDefSearchResult> activityDefSearchResultList = iOaSysActivityDefService.queryActivityDefList(activityDefSearchParam);
            return ActivityRv.wrapSuccess(activityDefSearchResultList);
        }
        catch (ActivityException ae){
            log.error("获取活动定义（含查询条件）失败：" + ae.getErrno().getDesc());
            return ActivityRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("获取活动定义（含查询条件）失败：" + ActivityErrorCode.unKnow.getDesc());
            return ActivityRv.wrapErrorCode(ActivityErrorCode.unKnow);
        }
    }

    /**
     * 创建活动定义
     * */
    @ApiOperation(value = "创建活动定义", notes = "创建活动定义")
    @PostMapping(value = "/createActivityDef")
    public ActivityRv createActivityDef(@RequestBody ActivityCreateParam activityCreateParam) {
        try {
            iOaSysActivityDefService.createActivityDef(activityCreateParam);
            return ActivityRv.wrapSuccess();
        }
        catch (ActivityException ae){
            log.error("创建活动定义：" + ae.getErrno().getDesc());
            return ActivityRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("创建活动定义" + ActivityErrorCode.unKnow.getDesc());
            return ActivityRv.wrapErrorCode(ActivityErrorCode.unKnow);
        }
    }

    /**
     * 查询活动信息（根据活动定义ID）
     *
     * @return
     */
    @ApiOperation(value = "查询活动信息（根据活动定义ID）", notes = "查询活动信息（根据活动定义ID）")
    @GetMapping(value = "/queryActivityInfo")
    public ActivityRv queryActivityInfo(@RequestParam("id") String id) {
        try {
            ActivityInfoResult activityInfoResult = iOaSysActivityDefService.queryActivityInfo(id);
            return ActivityRv.wrapSuccess(activityInfoResult);
        } catch (ActivityException ae) {
            log.error("查询活动信息（根据活动定义ID）：" + ae.getErrno().getDesc());
            return ActivityRv.wrapErrorCode(ae.getErrno());
        } catch (Exception ex) {
            log.error("查询活动信息（根据活动定义ID）：" + ActivityErrorCode.unKnow.getDesc());
            return ActivityRv.wrapErrorCode(ActivityErrorCode.unKnow);
        }
    }

    /**
     * 更新活动状态，包括活动定义和活动周期
     * 定时任务调用，每隔1分钟调用1次
     * @return
     */
    @ApiOperation(value = "更新活动状态", notes = "更新活动状态，包括活动定义和活动周期")
    @PostMapping(value = "/updateActivityStatus")
    public ActivityRv updateActivityStatus() {
        try {
            iOaSysActivityDefService.updateActivityStatus();
            return ActivityRv.wrapSuccess();
        } catch (ActivityException ae) {
            log.error("更新活动状态-失败：" + ae.getErrno().getDesc());
            return ActivityRv.wrapErrorCode(ae.getErrno());
        } catch (Exception ex) {
            log.error("更新活动状态-失败" + ActivityErrorCode.unKnow.getDesc());
            return ActivityRv.wrapErrorCode(ActivityErrorCode.unKnow);
        }
    }
}
